{% load i18n %}
<div class="form-actions well well-sm">
    <input type="hidden" id="action" name="action" value=""/>
    <input type="hidden" id="select-across" name="select_across" value=""/>

    <!--#2018/08/09 新增短信模板页面添加发送按钮 -->
    <div class="btn-group" style="display:none">
        <a class="dropdown-toggle btn btn-success" data-toggle="dropdown" href="#">
            <i id="send-icon" class="fa fa-share-square-o"></i>
            <span>发送短信</span>
        </a>
    </div>

    <div id="selectTemBtn" class="btn-group" style="display:none">
        <a class="dropdown-toggle btn btn-success" data-toggle="dropdown" href="#">
            <i id="send-icon" class="fa fa-share-square-o"></i>
            <span>选择模板</span>
        </a>
        <span style="margin-top: 8px;margin-left:6px;display: inline-block">Ps:不勾选则默认手机号全选</span>
    </div>

    <div id='delDiv' class="btn-group clearfix dropup">
        <a class="dropdown-toggle btn btn-success" data-toggle="dropdown" href="#">
            <i class="fa fa-wrench"></i>
            <span class="action-counter">{{ selection_note }}</span>
            <span class="all" style="display: none;">{{ selection_note_all }}</span>
            <span class="caret"></span></a>
        <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel">
            {% for name, descr, icon in action_choices %}
                <li><a onclick="$.do_action('{{ name }}');"><i class="{{ icon }}"></i> {{ descr }}</a></li>
            {% endfor %}
        </ul>
    </div>


    {% csrf_token %}
    {% if actions_selection_counter %}
        {% if cl.result_count != cl.result_list|length %}
            <a class="question btn btn-default" href="javascript:;" style="display: none;"
               title="{% trans "Click here to select the objects across all pages" %}">
                {% blocktrans with cl.result_count as total_count %}Select all {{ total_count }}
                    {{ model_name }}{% endblocktrans %}</a>
            <a class="clear btn btn-default" href="javascript:;"
               style="display: none;">{% trans "Clear selection" %}</a>
        {% endif %}
    {% endif %}
    <script type="text/javascript">var _actions_icnt = "{{ cl.result_list|length|default:"0" }}";</script>
</div>

<!-- 确认发送模态框（Modal） -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title" id="myModalLabel">
                    确认发送
                </h4>
            </div>
            <div id="confirmText" class="modal-body">
                在这里添加一些文本
            </div>
            <div id="templeteText" style="border: 1px solid">
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                </button>
                <button type="button" class="btn btn-primary" id="confirmBtn">
                    确认并发送
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

<!-- 选择短信模板模态框（Modal） -->
<!-- 2018/08/20 新增短信选择模板 -->
<div class="modal fade" id="templeteModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                    &times;
                </button>
                <h4 class="modal-title">
                    确认发送
                </h4>
            </div>
            <div id="templeteTable" class="results table-responsive">
                <table class="table table-bordered table-striped table-hover">
                    <thead>
                    <th scope="col" class="action-checkbox-column"><input type="checkbox" id="action-toggle"></th>


                    <th scope="col" class="sortable sorted ascending">

                        <div class="pull-right">
                            <a class="toggle" href="?o=-id"><i class="fa fa-sort-up"></i></a>
                        </div>
                        <div class="dropdown pull-left">
                            <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                                id
                            </a>
                            <ul class="dropdown-menu" role="menu">
                                <li class="active"><a href="?o=id" class="active"><i class="fa fa-caret-up"></i> 正序</a>
                                </li>
                                <li><a href="?o=-id" class="active"><i class="fa fa-caret-down"></i> 倒序</a></li>
                                <li><a href="?o=" class="active"><i class="fa fa-times"></i> 取消排序</a></li>
                            </ul>
                        </div>

                    </th>

                    <th scope="col" class="sortable">


                        <div class="dropdown pull-left">
                            <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                                短信模板
                            </a>
                            <ul class="dropdown-menu" role="menu">

                                <li><a href="?o=company.id" class="active"><i class="fa fa-caret-up"></i> 正序</a></li>

                                <li><a href="?o=-company.id" class="active"><i class="fa fa-caret-down"></i> 倒序</a></li>

                            </ul>
                        </div>

                    </th>

                    <th scope="col" class="sortable">


                        <div class="dropdown pull-left">
                            <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                                短信签名
                            </a>
                            <ul class="dropdown-menu" role="menu">

                                <li><a href="?o=company.id" class="active"><i class="fa fa-caret-up"></i> 正序</a></li>

                                <li><a href="?o=-company.id" class="active"><i class="fa fa-caret-down"></i> 倒序</a></li>

                            </ul>
                        </div>

                    </th>

                    <th scope="col" class="sortable">


                        <div class="dropdown pull-left">
                            <a class="dropdown-toggle" data-toggle="dropdown" href="#">
                                模板code
                            </a>
                            <ul class="dropdown-menu" role="menu">

                                <li><a href="?o=company.id" class="active"><i class="fa fa-caret-up"></i> 正序</a></li>

                                <li><a href="?o=-company.id" class="active"><i class="fa fa-caret-down"></i> 倒序</a></li>

                            </ul>
                        </div>

                    </th>
                    <tbody id="selTempleteTable">


                    </tbody>


                    </thead>

                </table>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                </button>`
                <button id="sendBtn" type="button" class="btn btn-primary">
                    确认并发送
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>


<script type="text/javascript">


    $(function () {
        //公司信息页面屏蔽原本的按钮，改为选择模板按钮
        if (window.location.href.indexOf('companyinfo') > 0) {

            $('#delDiv').remove();
            $('#selectTemBtn').show();
            console.log('URL:' + document.URL);
        }
        //公司信息页面点击选择模板按钮响应事件
        $('#selectTemBtn').click(function () {
            var mobileArr = [];
            var mobileJson = [];

            if ($("input:checkbox[name='_selected_action']:checked").length > 0&&$('#select-across').attr('value')==0) {

                $("input:checkbox[name='_selected_action']:checked").each(function () {
                    mobile = $(this).parent().parent().children().eq(3).text().replace(/\s/g, "").replace(/\n/g, "")    //去除换行和空格
                    mobileArr.push(mobile)
                })

            }
            if (mobileArr.length > 0) {
                mobileJson = JSON.stringify(mobileArr)
            }

            $('#templeteModal').modal('show');
            var modelURL = document.URL;
            var resultDatas = modelURL.split('/');
            var modelName = resultDatas[resultDatas.length - 2];
            $.ajax({
                url: '/admin/companyManage/selectMobile',
                type: 'post',
                data: {
                    csrfmiddlewaretoken: '{{ csrf_token }}',
                    mobileJson: mobileJson,
                    modelName: modelName
                },
                success: function (data) {
                    if (data.resu == "success") {
                        var tbodyText = ""
                        for (var i = 0; i < data.rows.length; i++) {
                            tbodyText = tbodyText + '<tr><td class="action-checkbox"><input type="checkbox" name="select_templete" value="10" class="action-select"></td><td>' + data.rows[i].id + '</td><td>' + data.rows[i].sign_name + '</td><td>' + data.rows[i].template + '</td><td>' + data.rows[i].template_code + '</td></tr>'

                        }
                        $('#selTempleteTable').html(tbodyText)

                    }
                    {#$(location).attr('href',"/admin/message_model/messagetemplet/?btn=showSend&mobileLen="+mobileLen);#}
                },
                error: function () {
                    alert('选择失败!请重试');
                }
            })
        })

        //选择短信模板后点击确认发送按钮响应事件
        $('#sendBtn').click(function () {

            if ($("input:checkbox[name='select_templete']:checked").length > 1) {
                alert('最多只能选择一个模板');
                return;
            } else if ($("input:checkbox[name='select_templete']:checked").length < 1) {
                alert(('请至少选择一个模板'))
                return;
            }
            $('#templeteModal').modal('hide');
            $('#myModal').modal("show");
            mobileLen = 0;
            if ($("input:checkbox[name='_selected_action']:checked").length > 0&&$('#select-across').attr('value')==0) {
                mobileLen = $("input:checkbox[name='_selected_action']:checked").length
            } else {
                mobileLen = parseInt($('#countMobile').text())
            }


            var confirm = "您已选择" + mobileLen + "个手机号码，选择的短信模板如下，请核对：";
            $('#confirmText').html(confirm);
            var checklist = $("input:checkbox[name='select_templete']:checked")[0];
            var templete = $(checklist).parent().parent().children().eq(3).text().replace(/\s/g, "").replace(/\n/g, "")

            $('#templeteText').html("&nbsp&nbsp&nbsp&nbsp&nbsp" + templete);


        })

        //确认发送模态框的确认发送按钮点击后响应
        $('#confirmBtn').click(function () {
            $('#myModal').modal("hide");
            var templeteCode = "";
            var templeteId = "";
            var signName = "";
            var templete = ""
            $("input:checkbox[name='select_templete']:checked").each(function () {
                templeteId = $(this).parent().parent().children().eq(1).text().replace(/\s/g, "").replace(/\n/g, "");
                signName = $(this).parent().parent().children().eq(2).text().replace(/\s/g, "").replace(/\n/g, "")
                templete = $(this).parent().parent().children().eq(3).text().replace(/\s/g, "").replace(/\n/g, "");
                templeteCode = $(this).parent().parent().children().eq(4).text().replace(/\s/g, "").replace(/\n/g, "");
            })
            mobileLen = 0
            if ($("input:checkbox[name='_selected_action']:checked").length > 0) {
                mobileLen = $("input:checkbox[name='_selected_action']:checked").length
            } else {
                mobileLen = parseInt($('#countMobile').text())
            }

            companyinfo = ""
             if (window.location.href.indexOf('ysbcompanyinfo') > 0) {
                    companyinfo = "易数宝公司信息"
             }else if(window.location.href.indexOf('baiduappcompanyinfo') > 0){
                    companyinfo = "百度商业应用公司信息"
             }else if(window.location.href.indexOf('clarkiiseocompanyinfo')>0){
                    companyinfo = "ClarkiiSEO公司信息"
             }else if(window.location.href.indexOf('smscompanyinfo')>0){
                    companyinfo = "SMS公司信息"
             }else if(window.location.href.indexOf('allcompanyinfo')>0){
                    companyinfo = "ALL公司信息"
             }

            $.ajax({
                url: '/admin/companyManage/sendTemplete',
                type: 'post',
                data: {
                    csrfmiddlewaretoken: '{{ csrf_token }}',
                    templeteCode: templeteCode,
                    templeteId: templeteId,
                    signName: signName,
                    templete: templete,
                    companyinfo : companyinfo,
                },
                success: function (data) {
                    alert((data.resu));
                    $(location).attr('href', "/admin/message_backup/sendresult");
                },
                error: function (data) {
                    alert("error");
                }
            })
        })


    })


</script>
